package com.cloudwise.dao.imp;

import com.cloudwise.dao.PersonInfoDao;
import com.cloudwise.util.BaseDao;

import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈
 * @date ：Created in 2021/1/25 14:33
 * @description：人员信息dao实现类
 * @modified By：
 * @version: 1
 */
public class PersonInfoDaoImpl extends BaseDao implements PersonInfoDao {
    @Override
    public List<Map<String, Object>> findAllPerson() {
        String sql = "select p.*,a.area_name from personinfo p inner join areainfo a on p.person_area_id = a.area_id";
        return executeQuery(sql);
    }

    @Override
    public List<Map<String, Object>> findAllArea() {
        return executeQuery("select * from areainfo");
    }

    @Override
    public Map<String, Object> findPersonById(int id) {
        String sql = "select * from personinfo where person_id=?";
        List<Map<String, Object>> mapList = executeQuery(sql, id);
        if (mapList != null && mapList.size() > 0) {
            return mapList.get(0);
        }
        return null;
    }

    @Override
    public boolean updatePerson(Map map) {
        String sql="update personinfo set person_name=?,person_area_id=?,person_age=?,person_address=? where person_id=?";
        boolean update = executeUpdate(sql, map.get("person_name"), map.get("person_area_id"), map.get("person_age"), map.get("person_address"), map.get("person_id"));
        return update;
    }
    @Override
    public boolean savePerson(Map map) {
        String sql="insert into personinfo  ( person_name, person_area_id, person_age, person_address)  values   (?,?,?,?)";
        boolean update = executeUpdate(sql, map.get("person_name"), map.get("person_area_id"), map.get("person_age"), map.get("person_address"));
        return update;
    }
    @Override
    public boolean deletePerson(Map map) {
        String sql="delete from personinfo where person_id=?";
        boolean update = executeUpdate(sql, map.get("person_id"));
        return update;
    }

    @Override
    public Map<String, Object> findPersonByIdAndName(Map map) {
        String sql="select * from personinfo where  person_id=? and person_name=?";
        List<Map<String, Object>> mapList = executeQuery(sql, map.get("person_id"), map.get("person_name"));
        if(mapList!=null&&mapList.size()>0){
            return mapList.get(0);
        }
        return null;
    }

    @Override
    public String findPersonName(String personId) {
        String sql="select person_name from personinfo where person_id=?";
        List<Map<String, Object>> mapList = executeQuery(sql, personId);
        if(mapList!=null&&mapList.size()>0){
            return mapList.get(0).get("person_name").toString();
        }
        return null;
    }
}
